package com.power.handler;

import cn.hutool.json.JSONUtil;
import com.power.result.R;
import com.power.util.ResponseUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author 海晨忆
 * @Des 登录失败回调
 * @Date 2025/8/7 9:08
 */
@Component
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        R result = R.FAIL(500, "登录失败：" + exception.getMessage());
        String jsonStr = JSONUtil.toJsonStr(result);
        ResponseUtils.write(response, jsonStr);
    }
}
